// miniprogram/heroChoose/heroChoose.js
var db = wx.cloud.database()
const app = getApp()
let cloudImage = require('../../utils/cloudImage.js')

Page({

  /**
   * 页面的初始数据
   */ 
  data: {
    heroClass: [],
    chooseedition:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  getData: function (e) {
    // db.collection("demohero").get({
    //   success: res => {
    //     console.log(res)
    //     this.setData({heroClass:res.data})
    //   },
    //   fail: console.error
    // })
  },

  finishUpload: function (e) {
    let {chooseedition} = this.data
    if(app.globalData.openId){
      db.collection('userScore').where({
        openId: app.globalData.openId,
      }).get({
        success: res => {
          let defaultList = ['demonhunter', 'druid', 'hunter', 'mage', 'paladin', 'priest', 'rogue', 'shaman', 'warlock', 'warrior', 'deathknight', 'neutral']
          for (var key in res.data[0][chooseedition]) {
            for (let j = 0; j < defaultList.length; j++) {
              if (key === defaultList[j]) {
                defaultList.splice(j, 1);
              }
            }
          }
          if (defaultList.length === 0) {
            wx.showModal({
              title: '完成评分',
              content: '感谢你对暴风城下的集结的评分，本次评分不用做商业用途，仅作交流，请放心提交。',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: '../pages/index/index',
                    });
                }
              }
            })
          }
          else {
            let newList = []
         
            defaultList.forEach(function (element) {
              newList.push(cloudImage.heroText(element))
            });
            let jumpHero=defaultList[0]
            let contentPlus =newList.toString()
            wx.showModal({
              title: '评分未完成',
              confirmText:'跳转',
              content: '您还有' + contentPlus+'未完成评分。',
              success(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    // url: '../cardJudge/cardJudge?hero=' + jumpHero,
                    url: '../cardJudge/cardJudge?hero=' + jumpHero+'&chooseEdition='+chooseedition,
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        }
      })
    }
    else{
      wx.showToast({
        title: '请完成任意卡的评分上传再点击此按钮',
        icon: 'none',
        duration: 2000
      })
    }
   
  },
  viewReport(){
    let {chooseedition} = this.data
    if(app.globalData.openId.length===0){
      wx.getUserProfile({desc:"正在获取",success:res=>{
             app.globalData.userInfo = res.userInfo
     wx.cloud.callFunction({name:'login'}).then( res => {
      let openid = res.result.openid;

      app.globalData.openId = openid
      wx.navigateTo({
        url: `../report/report?chooseEdition=${chooseedition}`,
      })
  }).catch(err => {
      console.log('登录失败', err)
    })   
      },fail:err=>console.error(err)})

    }
    else{
      wx.navigateTo({
        url: `../report/report?chooseEdition=${chooseedition}`,
      })
    }
   
  },
  getOpenid(e) {
    let that = this;
    wx.cloud.callFunction({
      name: 'login',
      success: res => {
        // console.log('云函数获取到的openid:', res.result.openid)
        var openid = res.result.openid;
        // that.setData({
        //   openid: openid
        // })
        app.globalData.openId = openid
        console.log(openid)
        db.collection('userScore').where({
          openId: app.globalData.openId,
        }).get(
          {
            success: res => {
              console.log(res.data.length)
              if (res.data.length > 0) {
                wx.showToast({
                  title: '数据获取成功',
                  icon: 'none',
                  duration: 2000
                })
              }
              else {
                db.collection('userScore').add({
                  data: {
                    openId: openid,
                    nickName:app.globalData.userInfo.nickName,
                  },
                  success: function (res) {
                    // console.log(res)
                    wx.showToast({
                      title: '没有找到您的历史评分数据',
                      icon: 'none',
                      duration: 2000
                    })
                  }
                })
              }
           
            },
            fail: console.error
          }
        )

      },
      fail: res => {
        console.log('登录失败', res)
      }
    })
  },

  historyGet:function(e){
    console.log(app.globalData.openId)
    if(app.globalData.openId.length===0){
      wx.getUserProfile({
        desc:'正在获取',//不写不弹提示框
        success:function(res){
          // console.log('获取成功: ',res)
          app.globalData.userInfo = res.userInfo
          wx.cloud.callFunction({
            name: 'login',
            success: res => {
              // console.log('云函数获取到的openid:', res.result.openid)
              var openid = res.result.openid;
              // that.setData({
              //   openid: openid
              // })
              app.globalData.openId = openid
              console.log(openid)
              db.collection('userScore').where({
                openId: app.globalData.openId,
              }).get(
                {
                  success: res => {
                    console.log(res.data.length)
                    if (res.data.length > 0) {
                      wx.showToast({
                        title: '数据获取成功',
                        icon: 'none',
                        duration: 2000
                      })
                    }
                    else {
                      db.collection('userScore').add({
                        data: {
                          openId: openid,
                          nickName:app.globalData.userInfo.nickName,
                        },
                        success: function (res) {
                          // console.log(res)
                          wx.showToast({
                            title: '没有找到您的历史评分数据',
                            icon: 'none',
                            duration: 2000
                          })
                        }
                      })
                    }
                 
                  },
                  fail: console.error
                }
              )
      
            },
            fail: res => {
              console.log('登录失败', res)
            }
          })
          // app.globalData.firstLogin= true
          // that.setData({hasUserInfo:true})
       
        },
        fail:function(err){
          
          wx.showToast({
            title: '获取数据失败',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
    else{
      wx.showToast({
        title: '已进行过数据更新',
        icon: 'none',
        duration: 2000
      })
    }
   
  },



    onLoad: function (options) {
      console.log(options)
      if (app.globalData.firstLogin) {
        this.setData({chooseedition:options.selectedition})

        wx.showModal({
          title: '评分提示',
          content: '1.评分1-5(支持0.5)\r\n2.短按图片放大，长按卡图则评论。左右滑动查看衍生卡（单机衍生卡放大）\r\n3.有意见或建议微博私信@一缕v秋凉/@抑郁型GE \r\n4.收集的数据仅供娱乐不作商业用途',
          showCancel: false,
        })
      }
      else {
        this.setData({chooseedition:options.selectedition})
      }
      db.collection("demohero").orderBy('order','asc').get({
        success: res => {        
          console.log(res)
          this.setData({ heroClass: res.data })
          console.log(res.data)
        },
        fail: console.error
      })
    },

    typeClick: function (e) {
      let chooseIndex = e.currentTarget.dataset.index
      let chooseHero = e.currentTarget.dataset.hero
      let {chooseedition} = this.data
      wx.navigateTo({
        url: '../cardJudge/cardJudge?selectIndex=' + chooseIndex + '&hero=' + chooseHero+'&chooseEdition='+chooseedition,
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
  })